import {
    addSupplier,
    deleteById,
    getAirSupplierList,
    getById,
    getCoSupplierList,
    getDecSupplierList,
    getExpSupplierList,
    getInspectionSupplierList,
    getInsSupplierList,
    getListForPage,
    getStoSupplierList,
    getSupplierList,
    getTraSupplierList,
    updateSupplier,
    addSupplierPayee,
    deleteSupplierPayeeById,
    getSupplierPayeeById,
    getPayeeListForPage
} from '../../services/logistics/logisticsSupplier';
import {message} from 'antd';
import Constants from '../../utils/Constants'
const R = require('ramda');

export default {
    namespace: 'logisticsSupplier',
    state: {
        page: {},
        list: [],
        supplierList: [],       //全部供应商
        traileList: [],         //拖车供应商
        declareList: [],        //报关供应商
        airList: [],            //空运供应商
        insuranceList: [],      //保险供应商
        inspectionList: [],     //商检供应商
        storageList: [],        //仓内装供应商
        coList: [],             //产地证供应商
        expressList: [],        //快递供应商
        dataSource: {},
        settlementUnitName: [],  //结算单位
        recAccPage: {},     //供应商收款人page
        recAccList: [],     //供应商收款人
        recAccData: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, {payload}) {
            return {...state, ...payload};
        }
    },
    effects: {
        /**
         * 分页获取供应商列表
         *
         * @param call
         * @param put
         *
         */
        *getListForPage({payload}, {call, put}) {
            const res = yield call(getListForPage, {...R.omit(['onSuccess', 'onFail'])(payload)});

            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id获取供应商
         *
         * @param id
         */
        *getById({payload}, {call, put}) {
            if (payload.id) {
                const res = yield call(getById, {...R.omit(['onSuccess', 'onFail'])(payload)});
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
        /**
         * 新增供应商
         *
         * @param call
         */
        *addSupplier({payload}, {call}) {
            const res = yield call(addSupplier, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("供应商新增成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 修改供应商
         *
         * @param call
         * @param put
         * @param select
         */
        *updateSupplier({payload}, {call, put}) {
            const res = yield call(updateSupplier, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("供应商更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 删除供应商
         *
         * @param id
         */
        *deleteById({payload}, {call}) {
            const res = yield call(deleteById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("供应商删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 查询供应商List
         *
         */
        *getSupplierList({ payload },{ call, put }){
            const res = yield call(getSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        supplierList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
                let unitName = [];
                if(res.data.content.list){
                    res.data.content.list.map(item => { unitName.push(item.companyName); })        
                }
                yield put({
                    type: 'refreshState',
                    payload: {
                        settlementUnitName: Array.from(new Set(unitName))
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 查询拖车供应商List
         *
         */
        *getTraSupplierList({ payload },{ call, put }){
            const res = yield call(getTraSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        traileList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 查询报关供应商List
         *
         */
        *getDecSupplierList({ payload },{ call, put }){
            const res = yield call(getDecSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        declareList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 查询空运供应商List
         *
         */
        *getAirSupplierList({ payload },{ call, put }){
            const res = yield call(getAirSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        airList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
          /**
         * 查询快递供应商List
         *
         */
        *getExpSupplierList({ payload },{ call, put }){
            const res = yield call(getExpSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        expressList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
          /**
         * 查询保险供应商List
         *
         */
        *getInsSupplierList({ payload },{ call, put }){
            const res = yield call(getInsSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        insuranceList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 查询仓内装供应商List
         *
         */
        *getStoSupplierList({ payload },{ call, put }){
            const res = yield call(getStoSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        storageList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
         /**
         * 查询商检供应商List
         *
         */
        *getInspectionSupplierList({ payload },{ call, put }){
            const res = yield call(getInspectionSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        inspectionList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 查询产地证供应商List
         *
         */
        *getCoSupplierList({ payload },{ call, put }){
            const res = yield call(getCoSupplierList, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type: 'refreshState',
                    payload:{
                        coList: R.isNil(res.data.content.list) ? [] : res.data.content.list
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },


        //----------------------------------物流供应商收款人
        /**
         * 分页获取收款人列表
         *
         * @param call
         * @param put
         *
         */
            *getPayeeListForPage({payload}, {call, put}) {
            const res = yield call(getPayeeListForPage, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        recAccList: res.data.content.list,
                        recAccPage: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id获取收款人信息
         *
         * @param id
         */
            *getSupplierPayeeById({payload}, {call, put}) {
            if (payload.id) {
                const res = yield call(getSupplierPayeeById, {...R.omit(['onSuccess', 'onFail'])(payload)});
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            recAccData: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        recAccData: {}
                    }
                });
            }
        },

        /**
         * 新增收款人
         *
         * @param call
         */
            *addSupplierPayee({payload}, {call}) {
            const res = yield call(addSupplierPayee, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("收款人新增成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 删除收款人
         *
         * @param id
         */
            *deleteSupplierPayeeById({payload}, {call}) {
            const res = yield call(deleteSupplierPayeeById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("收款人删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },


    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};